<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="采购单号" prop="vBuyNo">
        <el-input
          v-model="queryParams.vBuyNo"
          placeholder="请输入采购编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号" prop="vProjectNo">
        <el-input
          v-model="queryParams.vProjectNo"
          placeholder="请输入项目编号"
          clearable
          @change="vProjectNoChange(queryParams.vProjectNo)"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="applys">
        <el-select v-model="queryParams.applys" multiple placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="物料名称" prop="vSpaName">
        <el-input
          v-model="queryParams.vSpaName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="vSpaModel">
        <el-input
          v-model="queryParams.vSpaModel"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="vSpaBrand">
        <el-input
          v-model="queryParams.vSpaBrand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="spaList" height="450px">
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="addData(scope.row)"
            >添加</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="物料名称" align="center" prop="vSpaName" />
      <el-table-column label="规格型号" align="center" prop="vSpaModel" />
      <el-table-column label="品牌" align="center" prop="vSpaBrand" />
      <el-table-column label="数量" align="center" prop="nQty" />
      <el-table-column label="项目编号" align="center" prop="vProjectNo" />
      <el-table-column label="采购单号" align="center" prop="vBuyNo" />
      <!-- <el-table-column label="仓库" align="center" prop="vWhName" />
      <el-table-column label="库区" align="center" prop="vAreaName" /> -->
      <el-table-column label="库位" align="center" prop="vLocationName" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="submitForm">取 消</el-button>
    </div>
  </div>
</template>
<script>
import { getInvDataList, getApplys } from "@/api/inv/inv0005";
export default {
  name: "inv0005add",
  props: ["existData"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料表格数据
      spaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 经办人时间范围
      daterangeDExecute: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vSpaName: null,
        vSpaBrand: null,
        vSpaModel: null,
        vProjectNo: null,
        applys: null,
        vBuyNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //本次添加的数据
      addDataList: [],
      //已经添加的数据
      existDataList: [],
      options: [],
    };
  },
  created() {
    this.getList();

    this.addDataList = [];
  },
  mounted() {
    this.addDataList = [];
  },
  methods: {
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      getInvDataList(this.queryParams).then((response) => {
        this.spaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    vProjectNoChange(value) {
      this.queryParams.applys = null;
      getApplys({ value: value }).then((response) => {
        this.options = response.data;
      });
    },
    addData(row) {
      let obj1 = this.existData.find((existDataItem) => {
        return (
          row.vSpa == existDataItem.vSpa &&
          row.vSpaName == existDataItem.vSpaName &&
          row.vSpaModel == existDataItem.vSpaModel &&
          row.vSpaBrand == existDataItem.vSpaBrand &&
          row.vWhName == existDataItem.vWhName &&
          row.vAreaName == existDataItem.vAreaName &&
          row.vLocationName == existDataItem.vLocationName &&
          row.vProjectNo == existDataItem.vProjectNo &&
          row.vBuyNo == existDataItem.vBuyNo
        );
      });
      let obj = this.addDataList.find((addDataItem) => {
        return (
          row.vSpa == addDataItem.vSpa &&
          row.vSpaName == addDataItem.vSpaName &&
          row.vSpaModel == addDataItem.vSpaModel &&
          row.vSpaBrand == addDataItem.vSpaBrand &&
          row.vWhName == addDataItem.vWhName &&
          row.vAreaName == addDataItem.vAreaName &&
          row.vLocationName == addDataItem.vLocationName &&
          row.vProjectNo == addDataItem.vProjectNo &&
          row.vBuyNo == addDataItem.vBuyNo
        );
      });
      if (obj || obj1) {
        this.$message.error("该物料已存在，请重新确认");
      } else {
        this.addDataList.push(row);
        this.existData.push(row);
        this.$modal.msgSuccess("添加成功");
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 表单重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    submitForm() {
      console.log(this.addDataList);
      this.$emit("close", false, this.existData);
    },
  },
};
</script>
